package pay;

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

/**
 * 策略模式：实现商场支付的功能
 * 1：原价
 * 2：打折：6折、9折
 * 3：满减：满300减20  满700减50
 *
 * @author GongXincheng
 * @since 2019-09-13 20:46
 */
public class Main {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        // 1：输入商品信息.
        List<Item> itemList = scanner(sc);

        // 2：计算出优惠前的价格
        assert itemList != null;

        // 3：计算优惠后的价格
        double totalPrice = getTotalPrice(itemList);
        YouHuiContext youHuiContext = new YouHuiContext(YouHuiEnum.MANJIAN_300_20);
        System.out.println("总价：" + totalPrice);
        System.out.println("优惠后：" + youHuiContext.getResultPrice(totalPrice));

        sc.close();
    }

    /**
     * 输入商品信息.
     */
    private static List<Item> scanner(Scanner sc) {
        List<Item> result = new ArrayList<>();
        boolean flag = true;
        while (flag) {
            Item item = new Item();
            System.out.print("数量：");
            item.setNum(sc.nextInt());
            System.out.print("单价：");
            item.setPrice(sc.nextDouble());
            result.add(item);
            System.out.print("是否继续录入？(y/n)：");
            String next = sc.next().toLowerCase();
            if (!"y".equals(next)) {
                flag = false;
            }
        }
        return result;
    }

    /**
     * 获取未优惠前总价.
     */
    private static double getTotalPrice(List<Item> itemList) {
        return itemList.stream()
                .map(e -> e.getPrice() * e.getNum())
                .reduce((double) 0, Double::sum);
    }

}
